package com.zx.mes.hyl.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.mes.controller.BaseController;
import com.zx.mes.hyl.entity.Trole;
import com.zx.mes.hyl.service.TroleService;
import com.zx.mes.response.ObjectRestResponse;
import com.zx.mes.response.TableResultResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华云龙
 * @since 2018-07-02
 */
@CrossOrigin
@RestController
@RequestMapping("/role")
public class TroleController extends BaseController<TroleService,Trole> {

    @Override
    public ObjectRestResponse<Trole> add(Trole trole) {
        trole.setId(UUID.randomUUID().toString());
        return super.add(trole);
    }

    @Override
    public TableResultResponse<Trole> list(@RequestParam Map<String, Object> params, Trole entity) {
        int page = Integer.valueOf(params.get("page").toString());
        int limit = Integer.valueOf(params.get("limit").toString());

        TableResultResponse<Trole> response = null;

        EntityWrapper<Trole> wrapper = new EntityWrapper<>();
        if (entity != null && !"".equals(entity.getName())) {
            wrapper.like("name", entity.getName());
        }
        List<Trole> list = this.biz.selectPage(new Page(page, limit),wrapper).getRecords();
        if (list == null) {
            response = new TableResultResponse("无数据", 200, 0L, (long)page, (List)null);
        } else {
            response = new TableResultResponse("分页查询结果", 200, (long)this.biz.selectCount(wrapper), (long)page, list);
        }

        return response;
    }


    @Transactional
    @DeleteMapping("/deleteBatch")
    public ObjectRestResponse<String> deleteBatch(String ids) {
        ObjectRestResponse<String> response = new ObjectRestResponse<>();
        try {
            biz.deleteBatchIds(Arrays.asList(ids.split(",")));
        } catch (Exception e) {
            response.setMessage("批量删除角色失败:"+e.getMessage());
            response.setStatus(500);
            e.printStackTrace();
        }
        response.setMessage("批量删除角色成功!");
        return response;
    }

}

